package com.example.videopractice.mediaex

import android.Manifest
import android.graphics.ImageFormat
import android.hardware.Camera
import android.media.AudioFormat
import android.media.AudioRecord
import android.os.Bundle
import android.view.SurfaceHolder
import com.example.base.base.BaseBindingActivity
import com.example.base.util.ALog
import com.example.videopractice.databinding.ActivityVideoExBinding
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 视频录制实践
 * 用的是camera
 */
class VideoExActivity : BaseBindingActivity<ActivityVideoExBinding>() {

    private var camera: Camera? = null
    private val width = 640
    private val height = 480
    private val frame = 30

    private val sampleRateInHz: Int = 44100
    private val channelConfig: Int = AudioFormat.CHANNEL_CONFIGURATION_MONO
    private val audioFormat: Int = AudioFormat.ENCODING_PCM_16BIT
    private var audioRecord: AudioRecord? = null

    private var isStart = false
        set(value) {
            field = value
            binding.btnStart.isEnabled = !value
            binding.btnStop.isEnabled = value
        }

//    private val h264VideoEncoder = H264VideoEncoder(width, height, frame)
//    private val h264AudioEncoder = H264AudioEncoder(sampleRateInHz, channelConfig, audioFormat)
//    private val h264Muxer = H264Muxer()
    private val h264Encode = H264Encode()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestMultiplePermissions.launch(
            arrayOf(
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO
            )
        )

        camera = Camera.open()
        camera?.setDisplayOrientation(90)

        binding.surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(surfaceHolder: SurfaceHolder) {
                val parameter = camera?.parameters
                parameter?.previewFormat = ImageFormat.NV21
                parameter?.setPreviewSize(width, height)
                camera?.parameters = parameter
                camera?.setPreviewDisplay(surfaceHolder)
                camera?.setPreviewCallback { bytes, _ ->
                    //h264VideoEncoder.putData(bytes)
                    h264Encode.putVideoData(bytes)
                }
                camera?.startPreview()
            }

            override fun surfaceChanged(surfaceHolder: SurfaceHolder, p1: Int, p2: Int, p3: Int) { }

            override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) {
                camera?.setPreviewCallback(null)
                camera?.release()
            }
        })
        binding.btnStart.setOnClickListener {
            isStart = true
//            h264VideoEncoder.startEncoder()
//            h264AudioEncoder.startEncoder()
            h264Encode.startEncoder()
        }
        binding.btnStop.setOnClickListener {
            isStart = false
//            h264VideoEncoder.stopEncoder()
//            h264AudioEncoder.stopEncoder()
            h264Encode.stopEncoder()
        }
        binding.btnMuxer.setOnClickListener {
            GlobalScope.launch(IO) {
                try {
                    //h264Muxer.muxer()
                } catch (e: Exception) {
                    ALog.e("xiao","出错了")
                    e.printStackTrace()
                }
            }

        }
    }
}